﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class RoleService
    {
        private readonly RoleManager<IdentityRole> _roleManager;
        public RoleService(RoleManager<IdentityRole> roleManager)
        {
            _roleManager = roleManager;
        }

        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<IdentityRole>> FindEntitiesAsync(Expression<Func<IdentityRole,bool>> where)
        {
            var result= await _roleManager.Roles.Where(where).ToListAsync();
            return result;
        }

        /// <summary>
        /// 根据名称获取角色
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public async Task<IdentityRole> FindByNameAsync(string roleName)
        {
            var role = await _roleManager.FindByNameAsync(roleName);
            return role;
        }

        /// <summary>
        /// 根据角色Id获取角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IdentityRole> FindByIdAsync(string id)
        {
            var role=await _roleManager.FindByIdAsync(id);
            return role;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<IdentityResult> CreateAsync(IdentityRole role)
        {
            var result = await _roleManager.CreateAsync(role);
            return result;
        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<IdentityResult> UpdateAsync(IdentityRole role)
        {
            var result = await _roleManager.UpdateAsync(role);
            return result;
        }

        /// <summary>
        /// 角色是否存在
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public async Task<bool> RoleExistsAsync(string roleName)
        {
            var result = await _roleManager.RoleExistsAsync(roleName);
            return result;
        }

        /// <summary>
        /// 为角色添加Claim
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="claim">被验证主体特征的表述</param>
        /// <returns></returns>
        public async Task<IdentityResult> AddClaimAsync(IdentityRole role, Claim claim)
        {
            var result = await _roleManager.AddClaimAsync(role, claim);
            return result;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<IdentityResult> DeleteAsync(IdentityRole role)
        {
            var resule = await _roleManager.DeleteAsync(role);
            return resule;
        }

    }
}
